package org.example.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.IndicesClient;
import org.elasticsearch.client.RequestOptions;
import org.example.service.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
@Api(tags = "ES管理接口")
@RestController
public class ContentController {
    @Autowired
    private ContentService contentService;


    // 先从京东抓取数据并存入es中
    @ApiOperation(value = "抓取关键词数据")
    @GetMapping("/parse/{keyword}")
    public Boolean parse(@PathVariable("keyword") String keywords) throws Exception {
        return contentService.parseContent(keywords);
    }

    @HystrixCommand(fallbackMethod = "searchOther")
    // 在es中搜索数据返回结果
    @ApiOperation(value = "ES搜寻结果")
    @GetMapping("/search/{keyword}/{pageNo}/{pageSize}")
    public List<Map<String, Object>> search(@PathVariable("keyword") String keyword,
                                            @PathVariable("pageNo") int pageNo,
                                            @PathVariable("pageSize") int pageSize) throws IOException {
        return contentService.searchPage(keyword, pageNo, pageSize);
    }

    // 在es中搜索数据返回结果
    public List<Map<String, Object>> searchOther(@PathVariable("keyword") String keyword,
                                            @PathVariable("pageNo") int pageNo,
                                            @PathVariable("pageSize") int pageSize) throws IOException {
        HashMap<String, Object> map = new HashMap<>();
        map.put("空","暂无数据");
        List list = new LinkedList<>();
        list.add(map);
        return list;
    }

    @ApiOperation(value = "创建一个索引")
    @GetMapping("/create")
    void createIndex() throws IOException {
        contentService.createIndex();
    }
}